package versuch.client;

import javax.swing.ButtonGroup;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Versuch implements EntryPoint {
	
	public static boolean lock=false;
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private PollServiceAsync PollSvc = GWT
			.create(PollService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {



		FlexTable flexTable1 = new FlexTable();
		RootPanel.get("Main").add(flexTable1);
		
	//	setWidget(flexTable1);
		flexTable1.setSize("380px", "454px");

		Label lblNewLabel = new Label("Current Evaluation");
		lblNewLabel.setStyleName("StyleButton");
		lblNewLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		flexTable1.setWidget(2, 0, lblNewLabel);

		Label lblNewLabel_1 = new Label("Statistics");
		lblNewLabel_1.setStyleName("StyleButton");
		flexTable1.setWidget(2, 2, lblNewLabel_1);

		VerticalPanel verticalPanel = new VerticalPanel();
		flexTable1.setWidget(3, 0, verticalPanel);

		Button btnEvaluate = new Button("New button");
		btnEvaluate.setStyleName("ShadowButton");
		
		btnEvaluate.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(!lock){
					lock=true;
				
				start();
				}
			
			}
		});
		
		
		btnEvaluate.setText("Evaluate");
		verticalPanel.add(btnEvaluate);
		
	

	/*	Button button2 = new Button("New button");
		verticalPanel.add(button2);

		Button button3 = new Button("New button");
		verticalPanel.add(button3);
*/
		VerticalPanel verticalPanel_1 = new VerticalPanel();
		flexTable1.setWidget(3, 2, verticalPanel_1);

		Button button11 = new Button("New button");
		button11.setStyleName("ShadowButton");
		button11.setText("Lecture 1");
		verticalPanel_1.add(button11);
		button11.addStyleName(".StyleButton");
		button11.setSize("81", "28");

		Button button12 = new Button("New button");
		button12.setStyleName("ShadowButton");
		button12.setText("Lecture 2");
		verticalPanel_1.add(button12);
		button12.setSize("81", "28");

		Button button13 = new Button("New button");
		button13.setStyleName("ShadowButton");
		button13.setText("Lecture 3");
		verticalPanel_1.add(button13);
		button13.setSize("81", "28");

		Button button14 = new Button("New button");
		button14.setStyleName("ShadowButton");
		button14.setText("Lecture 4");
		verticalPanel_1.add(button14);
		button14.setSize("81", "28");

		Button button16 = new Button("New button");
		button16.setStyleName("ShadowButton");
		button16.setText("Lecture 5");
		verticalPanel_1.add(button16);
		button16.setSize("81", "28");

		Button button17 = new Button("New button");
		button17.setStyleName("ShadowButton");
		button17.setText("Lecture 6");
		verticalPanel_1.add(button17);
		button17.setSize("81", "28");

		Button button15 = new Button("New button");
		button15.setStyleName("ShadowButton");
		button15.setText("Lecture 7");
		verticalPanel_1.add(button15);
		button15.setSize("81", "28");
		
		Button button18 = new Button("New button");
		button18.setStyleName("ShadowButton");
		button18.setText("Lecture 8");
		verticalPanel_1.add(button18);
		button18.setSize("81", "28");
		
		Button button19 = new Button("New button");
		button19.setStyleName("ShadowButton");
		button19.setText("Lecture 9");
		verticalPanel_1.add(button19);
		button19.setSize("81", "28");
		
		Button button20 = new Button("New button");
		button20.setStyleName("ShadowButton");
		button20.setText("Lecture 10");
		verticalPanel_1.add(button20);
		button20.setSize("81", "28");

		Button button21 = new Button("New button");
		button21.setStyleName("ShadowButton");
		button21.setText("Lecture 11");
		verticalPanel_1.add(button21);
		button21.setSize("81", "28");

		Button button22 = new Button("New button");
		button22.setStyleName("ShadowButton");
		button22.setText("Lecture 12");
		verticalPanel_1.add(button22);
		button22.setSize("81", "28");
		flexTable1.getCellFormatter().setHorizontalAlignment(3, 2,
				HasHorizontalAlignment.ALIGN_CENTER);
		flexTable1.getCellFormatter().setVerticalAlignment(3, 2,
				HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable1.getCellFormatter().setHorizontalAlignment(3, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
		flexTable1.getCellFormatter().setHorizontalAlignment(2, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
		flexTable1.getCellFormatter().setHorizontalAlignment(2, 2,
				HasHorizontalAlignment.ALIGN_CENTER);

		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				sendNameToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendNameToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a
			 * response.
			 */
			private void sendNameToServer() {
				// First, we validate the input.
				// errorLabel.setText("");

				// Then, we send the input to the server.
				final Label textToServerLabel = new Label();
				final HTML serverResponseLabel = new HTML();

				serverResponseLabel.setText("");

			}
		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
	}
	
	
	
	

	
	

		public void start() {

			final DialogBox dialogBox = new DialogBox();
			dialogBox.show();
			RootPanel.get("Dialog").add(dialogBox);
			dialogBox.setAnimationEnabled(true);

			VerticalPanel verticalPanel = new VerticalPanel();
			dialogBox.setWidget(verticalPanel);
			verticalPanel.setSize("337px", "333px");

			Label lblNewLabel = new Label(
					"How satisfied are you with today's lecture? (overall rating)");
			verticalPanel.add(lblNewLabel);

			VerticalPanel verticalPanel_1 = new VerticalPanel();
			verticalPanel.add(verticalPanel_1);
			verticalPanel_1.setSize("133px", "120px");

			FlexTable flexTable = new FlexTable();
			verticalPanel_1.add(flexTable);
			flexTable.setSize("125px", "53px");

			final RadioButton rdbtnVerySatisfied = new RadioButton("new name",
					"very satisfied");
			flexTable.setWidget(0, 0, rdbtnVerySatisfied);

			final RadioButton rdbtnSatisfied = new RadioButton("new name",
					"satisfied");
			flexTable.setWidget(1, 0, rdbtnSatisfied);

			final RadioButton rdbtnNeutral = new RadioButton("new name",
					"neutral");
			flexTable.setWidget(2, 0, rdbtnNeutral);

			final RadioButton rdbtnUnsatisfied_1 = new RadioButton("new name",
					"unsatisfied");
			flexTable.setWidget(3, 0, rdbtnUnsatisfied_1);

			final RadioButton rdbtnUnsatisfied = new RadioButton("new name",
					"very unsatisfied");
			flexTable.setWidget(4, 0, rdbtnUnsatisfied);
			flexTable.getRowFormatter().setVerticalAlign(0,
					HasVerticalAlignment.ALIGN_MIDDLE);

			Label lblWhatDidYou = new Label("What did you like most?");
			verticalPanel.add(lblWhatDidYou);

			final TextArea textBox = new TextArea();
			verticalPanel.add(textBox);
			textBox.setSize("321px","50px");

			Label lblWhatDidYou_1 = new Label(
					"What did you dislike or did not understand at all?");
			verticalPanel.add(lblWhatDidYou_1);

			final TextArea textBox_1 = new TextArea();
			verticalPanel.add(textBox_1);
			textBox_1.setSize("321px","50px");

			HorizontalPanel horizontalPanel = new HorizontalPanel();
			horizontalPanel
					.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			verticalPanel.add(horizontalPanel);
			horizontalPanel.setWidth("329px");

			Button btnReset = new Button("Reset");
			btnReset.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					textBox.setText("");
					textBox_1.setText("");
					rdbtnNeutral.setValue(false);
					rdbtnSatisfied.setValue(false);
					rdbtnUnsatisfied.setValue(false);
					rdbtnUnsatisfied_1.setValue(false);
					rdbtnVerySatisfied.setValue(false);

				}
			});
			
	
			horizontalPanel.add(btnReset);
			btnReset.setSize("57", "28");

			Button btnSubmit = new Button("Submit");
			btnSubmit.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {

					String field1 = textBox.getText(); // Strings are stored in
														// "field1" and "field2"
					String field2 = textBox_1.getText();

					int intForServer = 0; // checked RadioButton is stored in
											// numbers 1 to 5

					if (rdbtnVerySatisfied.getValue() == true) {
						intForServer = 1;

					} else if (rdbtnSatisfied.getValue() == true) {
						intForServer = 2;
					} else if (rdbtnNeutral.getValue() == true) {
						intForServer = 3;
					} else if (rdbtnUnsatisfied_1.getValue() == true) {
						intForServer = 4;
					} else if (rdbtnUnsatisfied.getValue() == true) {
						intForServer = 5;
					}
					System.out.print(field1 + field2 + intForServer); // currently
																		// input
																		// is
																		// written
																		// on
																		// console

					lock=false;
					dialogBox.clear();
					dialogBox.setVisible(false);
					
					if(!CookieManager.cookieExists())
					{
						this.submitFeedback(new Feedback(new Lecture(1, "EIST", new Lecturer("foo", "bar")), intForServer, field1, field2));
						
						System.out.println("After DB call");
						CookieManager.setCookie();
						
						
					}
					
					else
					{
						System.out.println("Cookie already set!");
						
					}

				}
				
				private void submitFeedback(Feedback feedback) {
				    // Initialize the service proxy.
				    if (PollSvc == null) {
				      PollSvc = GWT.create(PollService.class);
				    }

				    // Set up the callback object.
				    AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
				      public void onFailure(Throwable caught) {
				        System.out.println("FAILURE");
				      }

				      public void onSuccess(Boolean result) {
				        //TODO: What to do when finished?
				    	  System.out.println("Connection successfully established, data saved!");
				      }
				    };

				    // Make the call to the stock price service.
				    PollSvc.submitFeedback(feedback, callback);
				  }
			});

			horizontalPanel.add(btnSubmit);
			btnSubmit.setSize("57", "28");
			

		
			  final Button btnClose = new Button("Close");
				
				btnClose.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						lock=false;
				dialogBox.clear();
				dialogBox.setVisible(false);		
						
			}
		});
				

			 horizontalPanel.add(btnClose); btnClose.setSize("57", "28");
			 

		}
	}

